package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleCollectionMapper;
import com.heima.article.service.ApArticleCollectionService;
import com.heima.article.service.ApArticleService;
import com.heima.common.constants.BehaviorConstans;
import com.heima.common.redis.CacheService;
import com.heima.model.article.dtos.ApArticleCollctionDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleCollection;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.message.Message;
import com.heima.utils.thread.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ApArticleCollectionServiceImpl extends ServiceImpl<ApArticleCollectionMapper, ApArticleCollection> implements ApArticleCollectionService {
    @Autowired
    private ApArticleService apArticleService;
    @Autowired
    private CacheService cacheService;
    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    /**
     * 文章收藏
     * @param dto
     * @return
     */
    @Override
    public ResponseResult collect(ApArticleCollctionDto dto) {
        if (ThreadLocalUtil.get() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        if (dto == null || dto.getEntryId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApArticle apArticle = apArticleService.getById(dto.getEntryId());
        int collect = 0;
        if (dto.getOperation() == BehaviorConstans.NOT_COLLECTION) {
            collect = -1;
            remove(Wrappers.<ApArticleCollection>lambdaQuery().eq(ApArticleCollection::getArticleId, dto.getEntryId()));
            cacheService.hDelete(ThreadLocalUtil.get() + "_" + dto.getEntryId() + BehaviorConstans.CACHE_END, "collection");
        } else if (dto.getOperation() == BehaviorConstans.COLLECTION) {
            collect = 1;
            ApArticleCollection apArticleCollection = new ApArticleCollection();
            apArticleCollection.setArticleId(dto.getEntryId());
            apArticleCollection.setUserId(ThreadLocalUtil.get());
            apArticleCollection.setPublishedTime(apArticle.getPublishTime());
            save(apArticleCollection);
            cacheService.hPut(ThreadLocalUtil.get() + "_" + dto.getEntryId() + BehaviorConstans.CACHE_END, "collection", "0");
        }
        Message message = new Message(dto.getEntryId());
        message.setCollect(collect);
        kafkaTemplate.send(BehaviorConstans.HOT_ARTICLE_AGG_TOPIC, JSON.toJSONString(message));
        log.info("成功发送:{}",message);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
